package com.it.controller;

import com.it.dao.SongListMapper;
import com.it.dao.TagsMapper;
import com.it.domain.TagMap;
import com.it.domain.Tags;
import com.it.service.LoginService;
import com.it.service.TagMapService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.List;

@RequestMapping("/tagMap")
@RestController
public class TagMapController {

    @Autowired
    private TagsMapper tagsMapper;

    @Autowired
    private TagMapService tagMapService;

    @PostMapping("/AddTagMap")
    public Integer AddTagMap(@RequestParam Integer targetType, @RequestParam List<Integer> tagId
    ,@RequestParam Integer targetId){

        return tagMapService.AddTagMap(tagId,targetType,targetId);
    }

    @PostMapping("/UpdateTagMap")
    public void UpdateTagMap(@RequestParam Integer targetType, @RequestParam Integer[] tagId
    ,@RequestParam Integer targetId){
        if(tagId.length == 0){
            return;
        }else{
            tagMapService.UpdateMap(Arrays.asList(tagId),targetType,targetId);
        }
    }

    @PostMapping("/SelectTag")
    public List<Integer> SelectTag(@RequestParam Integer targetId,@RequestParam Integer targetType){
        List<Integer> tags = tagMapService.SelectTagMap(targetId,targetType);
        if(tags.size() == 0){
            return null;
        }
        else {
            return tags;
        }
    }

    @PostMapping("/SelectByTagType")
    public List<Tags> SelectByTagType(@RequestParam Integer targetType){

        List<Integer> tagIds = tagMapService.SelectByTagType(targetType);
        return tagsMapper.getTags(tagIds);
    }

    @PostMapping("/SelectMainTag")
    public List<Integer> SelectMainTag(@RequestParam Integer targetType, @RequestParam Integer tagId){
        List<Integer> targetIds = tagMapService.SelectMainTag(targetType,tagId);
        return targetIds;
    }

    @PostMapping("/SelectListTag")
    public List<TagMap> SelectListTag(@RequestParam Integer targetType, @RequestParam Integer targetId){
        List<TagMap> TagMaps = tagMapService.SelectListTag(targetType,targetId);
        if(TagMaps.size() == 0){
            return null;
        }
        return TagMaps;
    }
}
